package com.freeter.modules.HelpCategory.controller;

import java.util.Arrays;
import java.util.List;

import com.freeter.common.annotation.J2CacheEvit;
import com.freeter.common.annotation.SysLog;
import com.freeter.common.validator.ValidatorUtils;
import com.freeter.modules.sys.entity.SysMenuEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freeter.common.utils.JQPageInfo;
import com.freeter.common.mpextend.parser.ParseWrapper;
import com.freeter.common.utils.PageInfo;


import com.freeter.modules.HelpCategory.entity.HelpCategoryEntity;
import com.freeter.modules.HelpCategory.entity.view.HelpCategoryView;
import com.freeter.modules.HelpCategory.entity.model.HelpCategoryModel;

import com.freeter.modules.HelpCategory.service.HelpCategoryService;
import com.freeter.common.utils.PageUtils;
import com.freeter.common.utils.R;
import com.freeter.common.utils.MPUtil;



/**
 * 帮助中心分类
 * 后端接口
 * @author xuchen
 * @email 171998110@qq.com
 * @date 2019-09-07 15:47:30
 */
@RestController
@RequestMapping("HelpCategory/helpcategory")
@SuppressWarnings({"unchecked","rawtypes"})
public class HelpCategoryController {
    @Autowired
    private HelpCategoryService helpCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    @RequiresPermissions("HelpCategory:helpcategory:list")
    public PageUtils page(JQPageInfo jqPageInfo,HelpCategoryModel helpCategory){
        EntityWrapper< HelpCategoryEntity> ew = ParseWrapper.parseWrapper(helpCategory);
     	PageInfo pageInfo = new PageInfo(jqPageInfo);
     	PageUtils page = helpCategoryService.queryPage(pageInfo, ew);
        return page;
        
    }

	/**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("HelpCategory:helpcategory:list")
    public R list( HelpCategoryModel helpCategory){
       	EntityWrapper< HelpCategoryEntity> ew = ParseWrapper.parseWrapper(helpCategory);
        return R.ok().put("data",  helpCategoryService.selectListView(ew));
    }

    /**
     * 列表
     */
    @RequestMapping("/list/tree")
    @RequiresPermissions("HelpCategory:helpcategory:list")
    public List<HelpCategoryView> list1( HelpCategoryModel helpCategory){
       	EntityWrapper< HelpCategoryEntity> ew = ParseWrapper.parseWrapper(helpCategory);
        List<HelpCategoryView> helpCategoryViews = helpCategoryService.selectListView(ew);
        helpCategoryViews.forEach(helpCategoryView -> {
            Integer pid = helpCategoryView.getPid();
            HelpCategoryEntity helpCategoryEntity = helpCategoryService.selectById(pid);
            if (helpCategoryEntity!=null){
                helpCategoryView.setParentName(helpCategoryEntity.getName());
            }
        });
        return helpCategoryViews;
    }

    @RequestMapping("/list/select")
    @RequiresPermissions("HelpCategory:helpcategory:list")
    public R select( HelpCategoryModel helpCategory){
       	EntityWrapper< HelpCategoryEntity> ew = ParseWrapper.parseWrapper(helpCategory);
        List<HelpCategoryView> helpCategoryViews = helpCategoryService.selectListView(ew);
        //添加顶级菜单
        HelpCategoryView root = new HelpCategoryView();
        root.setId(0);
        root.setName("一级菜单");
        root.setPid(-1);
        root.setOpen(true);
        helpCategoryViews.add(root);
        return R.ok().put("menuList", helpCategoryViews);
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    @RequiresPermissions("HelpCategory:helpcategory:info")
    public R query(HelpCategoryEntity helpCategory){
        EntityWrapper< HelpCategoryEntity> ew = ParseWrapper.parseWrapper(helpCategory);
		HelpCategoryView  helpCategoryView =  helpCategoryService.selectView(ew);
		return R.ok("查询帮助中心分类成功").put("data",  helpCategoryView);
    }
	
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("HelpCategory:helpcategory:info")
    public R info(@PathVariable("id") Integer id){
        HelpCategoryEntity helpCategory = helpCategoryService.selectById(id);
        Integer pid = helpCategory.getPid();
        HelpCategoryEntity helpCategoryEntity = helpCategoryService.selectById(pid);
        if (helpCategoryEntity!=null){
            helpCategory.setParentName(helpCategoryEntity.getName());
        }
        return R.ok().put("helpCategory", helpCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("HelpCategory:helpcategory:save")
    public R save(@RequestBody HelpCategoryEntity helpCategory){
    	ValidatorUtils.validateEntity(helpCategory);
        helpCategoryService.insert(helpCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("HelpCategory:helpcategory:update")
    public R update(@RequestBody HelpCategoryEntity helpCategory){
        ValidatorUtils.validateEntity(helpCategory);
        helpCategoryService.updateAllColumnById(helpCategory);//全部更新
        
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("HelpCategory:helpcategory:delete")
    public R delete(Integer menuId){

        //判断是否有子菜单或按钮
        List<HelpCategoryView> helpCategoryViews = helpCategoryService.queryListParentId(Long.valueOf(menuId));

        if(helpCategoryViews.size() > 0){
            return R.error("请先删除子菜单或按钮");
        }
        helpCategoryService.deleteById(menuId);

        return R.ok();
    }
}
